/*
 * Copyright 2002-2006 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 control.add(getTaskPanel(), "0, 0, 1, 3");
 * License for the specific language governing permissions and limitations under
 * the License.
 import com.jgoodies.forms.layout.FormLayout;
 */
package net.jwtools.tmt.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ButtonGroup;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JToggleButton;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.richclient.application.PageComponentContext;
import org.springframework.richclient.application.support.AbstractView;

import com.l2fprod.common.swing.JButtonBar;
import com.l2fprod.common.swing.plaf.blue.BlueishButtonBarUI;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class ButtonBarView extends AbstractView implements ApplicationListener {
	{
		// Set Look & Feel
		try {
			javax.swing.UIManager
					.setLookAndFeel("com.jgoodies.looks.plastic.Plastic3DLookAndFeel");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Default constructor.
	 */
	public ButtonBarView() {
	}

	protected void initListeners() {
		
	}

	/**
	 * Register the local command executors to be associated with named
	 * commands. This is called by the platform prior to making the view
	 * visible.
	 */
	protected void registerLocalCommandExecutors(PageComponentContext context) {
		// context.register("checkoutCommand", checkoutExecutor);
		// context.register(GlobalCommandIds.DELETE, deleteExecutor);
	}

	/**
	 * 
	 * @param e  event to process
	 */
	public void onApplicationEvent(ApplicationEvent e) {
		
	}

	@Override
	protected JComponent createControl() {
		JPanel control = new JPanel();
		
		JButtonBar jButtonBar;
		try {
			BorderLayout thisLayout = new BorderLayout();
			control.setLayout(thisLayout);
			control.setPreferredSize(new Dimension(400, 300));
			jButtonBar = new JButtonBar(JButtonBar.VERTICAL);
			control.add(jButtonBar, BorderLayout.WEST);
			jButtonBar.setUI(new BlueishButtonBarUI());

			jButtonBar.setPreferredSize(new java.awt.Dimension(100, 120));
			ButtonGroup group = new ButtonGroup();

			addButton(getMessage("homeView.buttonTitle"), getIconSource()
					.getIcon("homeBig.icon"), "homeView", jButtonBar, group);

			addButton(getMessage("checkinView.buttonTitle"), getIconSource()
					.getIcon("checkinBig.icon"), "checkinView", jButtonBar,
					group);

			addButton(getMessage("checkoutView.buttonTitle"), getIconSource()
					.getIcon("checkoutBig.icon"), "checkoutView", jButtonBar,
					group);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return control;
	}

	private void addButton(String title, Icon icon,
			final String viewDescriptorId, JButtonBar bar, ButtonGroup group) {
		final Action action = new AbstractAction(title, icon) {
			private static final long serialVersionUID = 1L;

			public void actionPerformed(ActionEvent e) {
				if (!getActiveWindow().getPage().getActiveComponent().getId()
						.equals(viewDescriptorId)) {
					getActiveWindow().getPage().showView(viewDescriptorId);
				}
			}
		};
		final JToggleButton button = new JToggleButton(action);
		button.addMouseListener(new MouseAdapter() { 
	          public void mouseEntered(MouseEvent me) { 
	              button.requestFocusInWindow();
	            } 
	          }); 

		bar.add(button);
		group.add(button);

		if (group.getSelection() == null) {
			button.setSelected(true);
		}

	}

	// private void show(Component component) {
	// if (currentComponent != null) {
	// remove(currentComponent);
	// }
	// add("Center", currentComponent = component);
	// revalidate();
	// repaint();
	// }

}